package zjh.msg.controller;

import zjh.msg.entity.User;
import zjh.msg.service.UserService;
import zjh.msg.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("username");
        String pwd = req.getParameter("password");
        User user = userService.login(name, pwd);
        if (user != null) {
            req.getSession().setAttribute("user", user);
            resp.sendRedirect("/list");
        }  else {
            req.setAttribute("erro", "账号或密码有错误");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
